{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "hired-chaos",
   "metadata": {},
   "source": [
    "# Latvian PVN (VAT) Numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "latin-extreme",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "hearing-tanzania",
   "metadata": {},
   "source": [
    "The function `clean_lv_pvn()` cleans a column containing Latvian PVN (VAT) number (PVN) strings, and standardizes them in a given format. The function `validate_lv_pvn()` validates either a single PVN strings, a column of PVN strings or a DataFrame of PVN strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "indirect-maria",
   "metadata": {},
   "source": [
    "PVN strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"16117519997\"\n",
    "* `standard`: PVN strings with proper whitespace in the proper places. Note that in the case of PVN, the compact format is the same as the standard one.\n",
    "* `birthdate`: split the date parts from the number and return the birth date, like \"1975-11-16\".\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_lv_pvn()` and `validate_lv_pvn()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dramatic-delight",
   "metadata": {},
   "source": [
    "### An example dataset containing PVN strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "amino-artist",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"pvn\": [\n",
    "            '161175-19997',\n",
    "            '40003521601',\n",
    "            '7542011030',\n",
    "            '7552A10004',\n",
    "            '8019010008',\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\",\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"1111 S Figueroa St, Los Angeles, CA 90015\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "visible-ecuador",
   "metadata": {},
   "source": [
    "## 1. Default `clean_lv_pvn`\n",
    "\n",
    "By default, `clean_lv_pvn` will clean pvn strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "historic-reflection",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_lv_pvn\n",
    "clean_lv_pvn(df, column = \"pvn\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "immediate-sewing",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "active-terry",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "running-absorption",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "weekly-bridal",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_lv_pvn(df, column = \"pvn\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "atmospheric-sitting",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "acknowledged-saskatchewan",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_lv_pvn(df, column = \"pvn\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "possible-mechanics",
   "metadata": {},
   "source": [
    "### `birthdate`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "attractive-default",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_lv_pvn(df, column = \"pvn\", output_format=\"birthdate\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "removed-upper",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned PVN strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "editorial-petroleum",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_lv_pvn(df, column=\"pvn\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "individual-adobe",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "greater-couple",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fresh-grounds",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_lv_pvn(df, \"pvn\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "nuclear-fellow",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "animal-ground",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_lv_pvn(df, \"pvn\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "major-alberta",
   "metadata": {},
   "source": [
    "## 4. `validate_lv_pvn()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "informed-estimate",
   "metadata": {},
   "source": [
    "`validate_lv_pvn()` returns `True` when the input is a valid PVN. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_lv_pvn()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_lv_pvn()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_lv_pvn()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "underlying-bronze",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_lv_pvn\n",
    "print(validate_lv_pvn('161175-19997'))\n",
    "print(validate_lv_pvn('40003521601'))\n",
    "print(validate_lv_pvn('7542011030'))\n",
    "print(validate_lv_pvn('7552A10004'))\n",
    "print(validate_lv_pvn('8019010008'))\n",
    "print(validate_lv_pvn(\"hello\"))\n",
    "print(validate_lv_pvn(np.nan))\n",
    "print(validate_lv_pvn(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "adjustable-testimony",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "heard-gabriel",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_lv_pvn(df[\"pvn\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "roman-sunrise",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "accessory-arrest",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_lv_pvn(df, column=\"pvn\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "outer-genius",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "hollow-match",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "validate_lv_pvn(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "robust-steel",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
